package com.eptonic.propmodifier;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import org.ini4j.Ini;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author zyc
 */
public class SpecifyActivity extends Activity {

    private final Random mRandom = new Random();
    private List<View> mRandomizeButtons = new ArrayList<View>(32);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.specify);

        /**
         * misc items
         */
        /** imei */
        final EditText etImei = findEditText(R.id.et_imei);
        final View btnRandomizeImei = findViewById(R.id.btn_randomize_imei);
        setupItem(etImei, btnRandomizeImei, Def.IMEI, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                mRandom.setSeed(System.currentTimeMillis());
                for (int i = 0; i < 15; i++) {
                    sb.append(mRandom.nextInt(10));
                }
                etImei.setText(sb.toString());
            }
        });

        /** mac-wifi-address */
        final EditText etMac = (EditText) findViewById(R.id.et_mac);
        final View btnRandomizeMac = findViewById(R.id.btn_randomize_mac);
        setupItem(etMac, btnRandomizeMac, Def.MAC, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                mRandom.setSeed(System.currentTimeMillis());
                for (int i = 0; i < 5; i++) {
                    sb.append(genHexStr() + ":");
                }
                sb.append(genHexStr());
                etMac.setText(sb.toString());
            }

            private String genHexStr() {
                String s = Integer.toHexString(mRandom.nextInt(256));
                if (s.length() < 1 || s.length() > 2) {
                    throw new IllegalStateException("hex str's length should be 1 or 2!");
                }
                return s.length() > 1 ? s : "0" + s;
            }
        });

        /** android_id */
        final EditText etAndroidId = findEditText(R.id.et_android_id);
        final View btnRandomizeAndroidId = findViewById(R.id.btn_randomize_android_id);
        setupItem(etAndroidId, btnRandomizeAndroidId, Def.ANDROID_ID, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                mRandom.setSeed(System.currentTimeMillis());
                for (int i = 0; i < 16; i++) {
                    sb.append(Integer.toHexString(mRandom.nextInt(16)));
                }
                etAndroidId.setText(sb.toString());
            }
        });

        /** sim_operator_numeric */
        final EditText etSimOperatorNumeric = findEditText(R.id.et_sim_operator_numeric);
        final View btnRandomizeSimOperatorNumeric = findViewById(R.id.btn_randomize_sim_operator_numeric);
        setupItem(etSimOperatorNumeric, btnRandomizeSimOperatorNumeric, Def.SIM_OPERATOR_NUMERIC, createGenericRandomizeCL(etSimOperatorNumeric));

        /** sim_operator_name */
        final EditText etSimOperatorName = findEditText(R.id.et_sim_operator_name);
        final View btnRandomizeSimOperatorName = findViewById(R.id.btn_randomize_sim_operator_name);
        setupItem(etSimOperatorName, btnRandomizeSimOperatorName, Def.SIM_OPERATOR_NAME, createGenericRandomizeCL(etSimOperatorName));

        /** sim_country_iso */
        final EditText etSimCountryIso = findEditText(R.id.et_sim_country_iso);
        final View btnRandomizeSimCountryIso = findViewById(R.id.btn_randomize_sim_country_iso);
        setupItem(etSimCountryIso, btnRandomizeSimCountryIso, Def.SIM_COUNTRY_ISO, createGenericRandomizeCL(etSimCountryIso));

        /** sim_state */
        final EditText etSimState = findEditText(R.id.et_sim_state);
        final View btnRandomizeSimState = findViewById(R.id.btn_randomize_sim_state);
        setupItem(etSimState, btnRandomizeSimState, Def.SIM_STATE, createGenericRandomizeCL(etSimState));

        /** phone_type */
        final EditText etPhoneType = findEditText(R.id.et_phone_type);
        final View btnRandomizePhoneType = findViewById(R.id.btn_randomize_phone_type);
        setupItem(etPhoneType, btnRandomizePhoneType, Def.PHONE_TYPE, createGenericRandomizeCL(etPhoneType));

        /** network_operator_name */
        final EditText etNetworkOperatorName = findEditText(R.id.et_network_operator_name);
        final View btnRandomizeNetworkOperatorName = findViewById(R.id.btn_randomize_network_operator_name);
        setupItem(etNetworkOperatorName, btnRandomizeNetworkOperatorName, Def.NETWORK_OPERATOR_NAME, createGenericRandomizeCL(etNetworkOperatorName));

        /** network_operator_numeric */
        final EditText etNetworkOperatorNumeric = findEditText(R.id.et_network_operator_numeric);
        final View btnRandomizeNetworkOperatorNumeric = findViewById(R.id.btn_randomize_network_operator_numeric);
        setupItem(etNetworkOperatorNumeric, btnRandomizeNetworkOperatorNumeric, Def.NETWORK_OPERATOR_NUMERIC, createGenericRandomizeCL(etNetworkOperatorNumeric));

        /** network_country_iso */
        final EditText etNetworkCountryIso = findEditText(R.id.et_network_country_iso);
        final View btnRandomizeNetworkCountryIso = findViewById(R.id.btn_randomize_network_country_iso);
        setupItem(etNetworkCountryIso, btnRandomizeNetworkCountryIso, Def.NETWORK_COUNTRY_ISO, createGenericRandomizeCL(etNetworkCountryIso));

        /** network_roaming */
        final EditText etNetworkRoaming = findEditText(R.id.et_network_roaming);
        final View btnRandomizeNetworkRoaming = findViewById(R.id.btn_randomize_network_roaming);
        setupItem(etNetworkRoaming, btnRandomizeNetworkRoaming, Def.NETWORK_ROAMING, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandom.setSeed(System.currentTimeMillis());
                etNetworkRoaming.setText(String.valueOf(mRandom.nextInt(2) != 0));
            }
        });

        /** network_type */
        final EditText etNetworkType = findEditText(R.id.et_network_type);
        final View btnRandomizeNetworkType = findViewById(R.id.btn_randomize_network_type);
        setupItem(etNetworkType, btnRandomizeNetworkType, Def.NETWORK_TYPE, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandom.setSeed(System.currentTimeMillis());
                etNetworkType.setText(String.valueOf(mRandom.nextInt(16)));
            }
        });

        /**
         * build.prop items
         */
        /** 1*5 */
        final EditText etModel = findEditText(R.id.et_model);
        final View btnRandomizeModel = findViewById(R.id.btn_randomize_model);
        setupItem(etModel, btnRandomizeModel, Def.MODEL, createGenericRandomizeCL(etModel));

        final EditText etBuildId = findEditText(R.id.et_build_id);
        final View btnRandomizeBuildId = findViewById(R.id.btn_randomize_build_id);
        setupItem(etBuildId, btnRandomizeBuildId, Def.BUILD_ID, createGenericRandomizeCL(etBuildId));

        final EditText etDisplayId = findEditText(R.id.et_display_id);
        final View btnRandomizeDisplayId = findViewById(R.id.btn_randomize_display_id);
        setupItem(etDisplayId, btnRandomizeDisplayId, Def.DISPLAY_ID, createGenericRandomizeCL(etDisplayId));

        final EditText etVerIncrem = findEditText(R.id.et_ver_increm);
        final View btnRandomizeVerIncrem = findViewById(R.id.btn_randomize_ver_increm);
        setupItem(etVerIncrem, btnRandomizeVerIncrem, Def.VER_INCREM, createGenericRandomizeCL(etVerIncrem));

        final EditText etSdk = findEditText(R.id.et_sdk);
        final View btnRandomizeSdk = findViewById(R.id.btn_randomize_sdk);
        setupItem(etSdk, btnRandomizeSdk, Def.SDK, createGenericRandomizeCL(etSdk));

        /** 2*5 */
        final EditText etCodeName = findEditText(R.id.et_code_name);
        final View btnRandomizeCodeName = findViewById(R.id.btn_randomize_code_name);
        setupItem(etCodeName, btnRandomizeCodeName, Def.CODE_NAME, createGenericRandomizeCL(etCodeName));

        final EditText etReleaseVersion = findEditText(R.id.et_release_version);
        final View btnRandomizeReleaseVersion = findViewById(R.id.btn_randomize_release_version);
        setupItem(etReleaseVersion, btnRandomizeReleaseVersion, Def.RELEASE_VERSION, createGenericRandomizeCL(etReleaseVersion));

        final EditText etDateUtc = findEditText(R.id.et_date_utc);
        final View btnRandomizeDateUtc = findViewById(R.id.btn_randomize_date_utc);
        setupItem(etDateUtc, btnRandomizeDateUtc, Def.DATE_UTC, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etDateUtc.setText(String.valueOf(System.currentTimeMillis() / 1000));
            }
        });

        final EditText etBuildType = findEditText(R.id.et_build_type);
        final View btnRandomizeBuildType = findViewById(R.id.btn_randomize_build_type);
        setupItem(etBuildType, btnRandomizeBuildType, Def.BUILD_TYPE, createGenericRandomizeCL(etBuildType));

        final EditText etUser = findEditText(R.id.et_user);
        final View btnRandomizeUser = findViewById(R.id.btn_randomize_user);
        setupItem(etUser, btnRandomizeUser, Def.USER, createGenericRandomizeCL(etUser));

        /** 3*5 */
        final EditText etHost = findEditText(R.id.et_host);
        final View btnRandomizeHost = findViewById(R.id.btn_randomize_host);
        setupItem(etHost, btnRandomizeHost, Def.HOST, createGenericRandomizeCL(etHost));

        final EditText etTags = findEditText(R.id.et_tags);
        final View btnRandomizeTags = findViewById(R.id.btn_randomize_tags);
        setupItem(etTags, btnRandomizeTags, Def.TAGS, createGenericRandomizeCL(etTags));

        final EditText etBrand = findEditText(R.id.et_brand);
        final View btnRandomizeBrand = findViewById(R.id.btn_randomize_brand);
        setupItem(etBrand, btnRandomizeBrand, Def.BRAND, createGenericRandomizeCL(etBrand));

        final EditText etProdutctName = findEditText(R.id.et_produtct_name);
        final View btnRandomizeProdutctName = findViewById(R.id.btn_randomize_produtct_name);
        setupItem(etProdutctName, btnRandomizeProdutctName, Def.PRODUTCT_NAME, createGenericRandomizeCL(etProdutctName));

        final EditText etDevice = findEditText(R.id.et_device);
        final View btnRandomizeDevice = findViewById(R.id.btn_randomize_device);
        setupItem(etDevice, btnRandomizeDevice, Def.DEVICE, createGenericRandomizeCL(etDevice));

        /** 4*5 */
        final EditText etBoard = findEditText(R.id.et_board);
        final View btnRandomizeBoard = findViewById(R.id.btn_randomize_board);
        setupItem(etBoard, btnRandomizeBoard, Def.BOARD, createGenericRandomizeCL(etBoard));

        final EditText etCpuAbi = findEditText(R.id.et_cpu_abi);
        final View btnRandomizeCpuAbi = findViewById(R.id.btn_randomize_cpu_abi);
        setupItem(etCpuAbi, btnRandomizeCpuAbi, Def.CPU_ABI, createGenericRandomizeCL(etCpuAbi));

        final EditText etFingerprint = findEditText(R.id.et_fingerprint);
        final View btnRandomizeFingerprint = findViewById(R.id.btn_randomize_fingerprint);
        setupItem(etFingerprint, btnRandomizeFingerprint, Def.FINGERPRINT, createGenericRandomizeCL(etFingerprint));

        /**
         * save button logic
         */
        final View btnSaveAll = findViewById(R.id.btn_save_all);
        btnSaveAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Ini ini = new Ini();

                /** misc items */
                putItem(Def.IMEI, etImei, ini);
                putItem(Def.MAC, etMac, ini);
                putItem(Def.ANDROID_ID, etAndroidId, ini);
                putItem(Def.SIM_OPERATOR_NUMERIC, etSimOperatorNumeric, ini);
                putItem(Def.SIM_OPERATOR_NAME, etSimOperatorName, ini);
                putItem(Def.SIM_COUNTRY_ISO, etSimCountryIso, ini);
                putItem(Def.SIM_STATE, etSimState, ini);
                putItem(Def.PHONE_TYPE, etPhoneType, ini);
                putItem(Def.NETWORK_OPERATOR_NAME, etNetworkOperatorName, ini);
                putItem(Def.NETWORK_OPERATOR_NUMERIC, etNetworkOperatorNumeric, ini);
                putItem(Def.NETWORK_COUNTRY_ISO, etNetworkCountryIso, ini);
                putItem(Def.NETWORK_ROAMING, etNetworkRoaming, ini);
                putItem(Def.NETWORK_TYPE, etNetworkType, ini);

                /** build.prop items */
                putItem(Def.MODEL, etModel, ini);
                putItem(Def.BUILD_ID, etBuildId, ini);
                putItem(Def.DISPLAY_ID, etDisplayId, ini);
                putItem(Def.VER_INCREM, etVerIncrem, ini);
                putItem(Def.SDK, etSdk, ini);

                putItem(Def.CODE_NAME, etCodeName, ini);
                putItem(Def.RELEASE_VERSION, etReleaseVersion, ini);
                putItem(Def.DATE_UTC, etDateUtc, ini);
                putItem(Def.BUILD_TYPE, etBuildType, ini);
                putItem(Def.USER, etUser, ini);

                putItem(Def.HOST, etHost, ini);
                putItem(Def.TAGS, etTags, ini);
                putItem(Def.BRAND, etBrand, ini);
                putItem(Def.PRODUTCT_NAME, etProdutctName, ini);
                putItem(Def.DEVICE, etDevice, ini);

                putItem(Def.BOARD, etBoard, ini);
                putItem(Def.CPU_ABI, etCpuAbi, ini);
                putItem(Def.FINGERPRINT, etFingerprint, ini);

                try {
                    ini.store(openFileOutput(Def.PROP_FILE_NAME, Context.MODE_WORLD_READABLE));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Util.Notifier.showLongToast("保存成功", SpecifyActivity.this);
            }
        });

        /**
         * randomize all and save
         */
        findViewById(R.id.btn_randomize_all_and_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (View randomizeButton : mRandomizeButtons) {
                    randomizeButton.performClick();
                }
                btnSaveAll.performClick();
            }
        });
    }

    private void setupItem(EditText editText, View btnRandomize, String key, View.OnClickListener onClickListener) {
        editText.setText(Util.loadFromFileByOurContext(this, key, "default_value"));
        btnRandomize.setOnClickListener(onClickListener);
        mRandomizeButtons.add(btnRandomize);
    }

    private void putItem(String key, EditText etItem, Ini ini) {
        ini.put(Def.SECTION_1, key, Util.getEditTextContent(etItem));
    }

    private EditText setupItem(int editTextViewId, String key, String defaultValue, int randomizeButtonViewId, View.OnClickListener randomizeClickListener) {
        final EditText editText = findEditText(editTextViewId);
        editText.setText(Util.loadFromFileByOurContext(this, key, defaultValue));
        final View randomizeButton = findViewById(randomizeButtonViewId);
        randomizeButton.setOnClickListener(randomizeClickListener);
        mRandomizeButtons.add(randomizeButton);
        return editText;
    }

    private View.OnClickListener createGenericRandomizeCL(final EditText editText) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                mRandom.setSeed(System.currentTimeMillis());
                for (int i = 0; i < 2; i++) {
                    sb.append(mRandom.nextInt(10));
                }
                editText.setText(sb.toString());
            }
        };
    }

    private EditText findEditText(int editTextViewId) {
        return (EditText) findViewById(editTextViewId);
    }

    private void promptEmpty(String field) {
        Util.Notifier.showLongToast(field + "不能为空", this);
    }
}
